package com.sws.worktive;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.sws.worktive.task.ServerDownload;
import com.sws.worktive.task.ServerResponse;
import com.sws.worktive.util.Constant;
import com.sws.worktive.util.WT_Prefrences;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SignUpScreen extends Fragment implements OnClickListener,ServerResponse{

	LinearLayout ll_back;
	Button btn_signup;
	EditText edt_con_password;
	EditText edt_password,edt_phno;
	WT_Prefrences wtprefences;
	List<NameValuePair> nameValuePairs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		
		
		View view = inflater.inflate(R.layout.signup_screen, container, false);
		TextView txtHeader = (TextView) view.findViewById(R.id.screen_title);
		btn_signup=(Button)view.findViewById(R.id.btn_signup);
		btn_signup.setOnClickListener(this);
		txtHeader.setText(getResources().getString(R.string.sign_up));
		wtprefences=new WT_Prefrences(getActivity());
		
		edt_password=(EditText)view.findViewById(R.id.edt_password);
		edt_phno=(EditText)view.findViewById(R.id.edt_phno);
		edt_con_password=(EditText)view.findViewById(R.id.edt_con_password);

		TelephonyManager tMgr = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
		String mPhoneNumber = tMgr.getLine1Number();
		if(mPhoneNumber!=null){
			edt_phno.setText(mPhoneNumber);
		}
		
		ll_back = (LinearLayout) view.findViewById(R.id.layout_back);
		ll_back.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layout_back:
			FragmentManager fragmentManager = getActivity()
					.getSupportFragmentManager();
			fragmentManager.popBackStack();
		break;
		case R.id.btn_signup:
			
			if(edt_phno.getText().toString().length()==0){
				Toast.makeText(getActivity(), getString(R.string.valPhone), Toast.LENGTH_SHORT).show();
			}else if(edt_password.getText().toString().length()==0){
				Toast.makeText(getActivity(), getString(R.string.valpassword), Toast.LENGTH_SHORT).show();
			}else if(!edt_password.getText().toString().equals(edt_con_password.getText().toString())){
				Toast.makeText(getActivity(), getString(R.string.valpassword), Toast.LENGTH_SHORT).show();
			}else{
				nameValuePairs = new ArrayList<NameValuePair>();
				nameValuePairs.add(new BasicNameValuePair("phoneNumber",
						edt_phno.getText().toString()));
				nameValuePairs.add(new BasicNameValuePair("password",
						edt_password.getText().toString().trim()));
				nameValuePairs.add(new BasicNameValuePair("devapipassword",
						Constant.PASSWORD_VALUE));

				((DrawerScreen) getActivity()).mProgressDialog = ProgressDialog
						.show(getActivity(), "", getActivity().getResources()
								.getString(R.string.waitregistering));
				ServerDownload serverDownload = new ServerDownload(
						getActivity(), SignUpScreen.this,
						Constant.SIGNUP_FRAGMENT);
				serverDownload.execute(nameValuePairs);
				
			}
			
		break;
		default:
			break;
		}
	}
	
	@Override
	public void httpResponse(JSONObject jsonObject, String requestTag) {
		// TODO Auto-generated method stub

		try {
			if (jsonObject != null && jsonObject instanceof JSONObject) {
				int status = jsonObject.getInt("status");
				if (status == 1) {
					if(((DrawerScreen) getActivity()).mProgressDialog!=null)
						((DrawerScreen) getActivity()).mProgressDialog.dismiss();
					
					Toast.makeText(getActivity(),
							jsonObject.getString("message"), Toast.LENGTH_SHORT)
							.show();
					JSONObject userObj = jsonObject.getJSONObject("user");
					if (userObj != null && userObj instanceof JSONObject) {
						int phoneNumber = userObj.getInt("phoneNumber");
						String token = userObj.getString("token");
						wtprefences.setToken(token);
						wtprefences.setPhonenumber(phoneNumber);
					}
					((DrawerScreen)getActivity()).addFragment(0);
				}else{
					if(((DrawerScreen) getActivity()).mProgressDialog!=null)
						((DrawerScreen) getActivity()).mProgressDialog.dismiss();
					
					Toast.makeText(getActivity(),
							jsonObject.getString("message"), Toast.LENGTH_SHORT)
							.show();
				}
			}
		} catch (Exception ex) {

		}

	};

	
}
